
import React from 'react';
import { Modal, Button,Form,Input } from 'antd';
import * as Utils from "../../utils/action";
import TextArea from 'antd/lib/input/TextArea';
const { confirm } = Modal;
class BasicConfirm extends React.Component {
  state = {
    inputValue:''
  };

  inputChange=(e)=>{
    this.setState({inputValue:e.target.value})
  }
   showConfirm=()=> {
     const p=this;
     let content='';
     if(p.props.content){
        if(p.props.content.type==='INPUT'){
          content=<Input onChange={this.inputChange} placeholder={p.props.content.placeholder}/>
        }else if(p.props.content.type==='TEXTAREA'){
          content=<TextArea onChange={this.inputChange} placeholder={p.props.content.placeholder}/>
        }
     }
     if( p.props.data instanceof Array?p.props.data.length:p.props.data){
      confirm({
        title:p.props.title,
        content: content,
        okText: '确认',
        okType: p.props.okType?p.props.okType:'primary',
        cancelText: '取消',
        onOk() {
          if(content){
            if(p.state.inputValue){
              p.props.handleConfirm(p.state.inputValue,p.props.api,p.props.data);
            }else{
              Utils.openNotification('warning',p.props.content.placeholder)
            }
          }else{
            p.props.handleConfirm(p.state.inputValue,p.props.api,p.props.data);
          }
          p.setState({inputValue:''})
         
        },
        onCancel() {p.setState({inputValue:''})},
      });
     }else{
      Utils.openNotification('warning','请选择需要操作的数据')
     }
      
  }
  render(){
    //button: 是否显示为button  buttonType:按钮类型(显示为按钮时) buttonName:显示的名字 api:需要调用的接口  data:传递的数据 
    //title ：弹出框的title content：需要填写值时  (INPUT,TEXTAREA) 目前仅限一个输入框  icon 按钮图标
    const {button,buttonType,buttonName,icon}=this.props;
    return(
      button?<Button type={buttonType?buttonType:'primary'} icon={icon?icon:''} onClick={this.showConfirm}>{buttonName}</Button>:
      <a onClick={this.showConfirm}>{buttonName}</a>
      
    )
  }
}


export default Form.create({})(BasicConfirm);
